﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_MSDS_Section02
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Translate()
        If Not IsPostBack Then

			Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
			If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
				ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
			Else
				Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
			End If
            If Not Request.QueryString("Id").ToString.Equals("0") Then
                hidMSDSID.Value = Request.QueryString("Id")
            End If
            CheckData()
        End If
    End Sub
    Protected Sub Translate()
        lblHeaderNhandang.Text = getTranslate("lblHeaderNhandang", Session("LanguageId"), lblHeaderNhandang.Text)
        lblHeaderThanhphan02.Text = getTranslate("lblHeaderThanhphan02", Session("LanguageId"), lblHeaderThanhphan02.Text)
        lblHeaderSoCAS02.Text = getTranslate("lblHeaderSoCAS02", Session("LanguageId"), lblHeaderSoCAS02.Text)
        lblHeaderCTHH02.Text = getTranslate("lblHeaderCTHH02", Session("LanguageId"), lblHeaderCTHH02.Text)
        lblHeaderPhantram02.Text = getTranslate("lblHeaderPhantram02", Session("LanguageId"), lblHeaderPhantram02.Text)
		'lblSection02Thanhphan1.Text = getTranslate("lblSection02Thanhphan1", Session("LanguageId"), lblSection02Thanhphan1.Text)
		'lblSection02Thanhphan2.Text = getTranslate("lblSection02Thanhphan2", Session("LanguageId"), lblSection02Thanhphan2.Text)
		'lblSection02Thanhphan3.Text = getTranslate("lblSection02Thanhphan3", Session("LanguageId"), lblSection02Thanhphan3.Text)
		'lblSection02Thanhphan4.Text = getTranslate("lblSection02Thanhphan4", Session("LanguageId"), lblSection02Thanhphan4.Text)
		'lblSection02Thanhphan5.Text = getTranslate("lblSection02Thanhphan5", Session("LanguageId"), lblSection02Thanhphan5.Text)
		ltrRequired.Text = getTranslate("ltrRequired", Session("LanguageId"), ltrRequired.Text)
		hidErrorSection02.Value = getTranslate("hidErrorSection02", Session("LanguageId"), hidErrorSection02.Value)
		'strRequiredFieldSoCAS.ErrorMessage = getTranslate("strRequiredFieldSoCAS", Session("LanguageId"), strRequiredFieldSoCAS.ErrorMessage)
		'strRequiredFieldFomula.ErrorMessage = getTranslate("strRequiredFieldFomula", Session("LanguageId"), strRequiredFieldFomula.ErrorMessage)
		'strRequiredFieldHamluong.ErrorMessage = getTranslate("strRequiredFieldHamluong", Session("LanguageId"), strRequiredFieldHamluong.ErrorMessage)
		'strRequiredFieldThanhphan1.ErrorMessage = getTranslate("strRequiredFieldThanhphan1", Session("LanguageId"), strRequiredFieldThanhphan1.ErrorMessage)

    End Sub

    Sub CheckData()
        Using data As New VSEntities
			'Dim LanguageId = Session("LanguageId").ToString()
			'Dim p = (From q In data.MSDSSection02
			'   Where q.LanguageId = LanguageId And q.MSDSId = hidMSDSID.Value
			'   ).FirstOrDefault()
			'If Not p Is Nothing Then
			'    txtS210.Text = p.S210
			'    txtS211.Text = p.S211
			'    txtS212.Text = p.S212
			'    txtS213.Text = p.S213

			'    txtS220.Text = p.S220
			'    txtS221.Text = p.S221
			'    txtS222.Text = p.S222
			'    txtS223.Text = p.S223

			'    txtS230.Text = p.S230
			'    txtS231.Text = p.S231
			'    txtS232.Text = p.S232
			'    txtS233.Text = p.S233

			'    txtS240.Text = p.S240
			'    txtS241.Text = p.S241
			'    txtS242.Text = p.S242
			'    txtS243.Text = p.S243

			'    txtS250.Text = p.S250
			'    txtS251.Text = p.S251
			'    txtS252.Text = p.S252
			'    txtS253.Text = p.S253

			'    hidID.Value = p.Section02Id
			'End If
			Dim p = (From q In data.Section02Detail Where q.Section02Id = hidMSDSID.Value).Count()
			hidMode.Value = If(p > 0, "1", "0")
        End Using
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Try
				'Dim p As MSDSSection02
				'p = (From q In data.MSDSSection02
				'Where q.Section02Id = hidID.Value
				'  ).FirstOrDefault()
				'Dim _isNew = True
				'If Not p Is Nothing Then
				'    _isNew = False
				'Else
				'    p = New MSDSSection02
				'End If
				'With p

				'    .S210 = txtS210.Text.Trim
				'    .S211 = txtS211.Text.Trim
				'    .S212 = txtS212.Text
				'    .S213 = txtS213.Text.Trim

				'    .S220 = txtS220.Text.Trim
				'    .S221 = txtS221.Text.Trim
				'    .S222 = txtS222.Text
				'    .S223 = txtS223.Text.Trim

				'    .S230 = txtS230.Text.Trim
				'    .S231 = txtS231.Text.Trim
				'    .S232 = txtS232.Text
				'    .S233 = txtS233.Text.Trim

				'    .S240 = txtS240.Text.Trim
				'    .S241 = txtS241.Text.Trim
				'    .S242 = txtS242.Text
				'    .S243 = txtS243.Text.Trim

				'    .S250 = txtS250.Text.Trim
				'    .S251 = txtS251.Text.Trim
				'    .S252 = txtS252.Text
				'    .S253 = txtS253.Text.Trim

				'    .LanguageId = Session("LanguageId")

				'End With
				'If _isNew Then
				'    p.MSDSId = hidMSDSID.Value
				'    p.Created = Now
				'    p.Creator = Session("UserName")
				'    data.MSDSSection02.AddObject(p)
				'Else
				'    p.Modified = Now
				'    p.Modifier = Session("UserName")
				'End If
				'data.SaveChanges()

				'' Xử lý phàn Detail
				'Delete all by Section03Id
				Dim _sqlString As String = " DELETE FROM SECTION02Detail WHERE Section02ID=" & hidMSDSID.Value
				data.ExecuteStoreCommand(_sqlString)
				'' Insert Section03Detail
				If hidValue.Value <> "" Then
					Dim listData = hidValue.Value.Split("$")
					For index = 0 To listData.Count - 1
						Dim _p As New Section02Detail
						If listData(index) <> "" Then
                            Dim _lst = listData(index).Split("@")
							If _lst.Length > 3 Then
								_p.Section02Id = hidMSDSID.Value
								_p.ThanhPhanNguyHiem = _lst(0)
								_p.SoCAS = _lst(1)
								_p.CongThucHoaHoc = _lst(2)
								_p.HamLuong = _lst(3)
								_p.LanguageID = Session("LanguageId")
								data.Section02Detail.AddObject(_p)
							End If
						End If
					Next
					data.SaveChanges()
				End If
				Insert_App_Log(If(hidMode.Value = "0", "Insert", "Update") & "  MSDS Section02:" & hidMSDSID.Value & "", Function_Name.MSDSOriginal, If(hidMode.Value = "0", Audit_Type.Create, Audit_Type.Edit), Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("AlertboxRedirect('Lưu dữ liệu thành công.','../../Page/MSDS/Section03.aspx?Id=" & hidMSDSID.Value & "');", Me.Page, True)

            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            Finally
                GC.Collect()
            End Try
        End Using
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        Response.Redirect("../../Page/MSDS/Section01.aspx?Id=" & hidMSDSID.Value)
    End Sub

#End Region

End Class
